package org.amn.service.impl;

import java.io.File;
import java.io.IOException;

import org.amn.dto.MsgDTO;
import org.amn.file.FileManager;
import org.amn.file.FileStatus;
import org.amn.file.FileUtils;
import org.amn.service.IMsgReceiver;

import com.bea.core.repackaged.springframework.util.Assert;

/**
 * <p>
 * 描述:接收消息服务实现类
 * </p>
 *
 * @see
 * @author yangjun1120@gmail.com
 *
 */
public class MsgReceiverPojo implements IMsgReceiver {

    private FileManager fm;

    public FileManager getFm() {
        return fm;
    }

    public void setFileManager(FileManager manager) {
        this.fm = manager;
    }

    /**
     * 保存消息
     */
    @Override
    public void receive(MsgDTO msg) throws IOException {
        Assert.notNull(msg, "Message is null!");
        persist(msg);

    }

    private void persist(MsgDTO msg) throws IOException {

        String newName = FileUtils.formatFileName(msg.getMsgId());

        String path = fm.getPath(FileStatus.PENDDING);

        String folder = FileUtils.getFolder();

        FileUtils.writeToFile(path + File.separator + folder + File.separator + newName, msg.getContent().getBytes());
    }
}
